package com.dkm.orproject.dkmoa.service.serviceimpl;

import com.dkm.orproject.dkmoa.dao.*;
import com.dkm.orproject.dkmoa.domain.*;
import com.dkm.orproject.dkmoa.domain.vo.OaAdressBookDtVo;
import com.dkm.orproject.dkmoa.domain.vo.OaAdressBookVo;
import com.dkm.orproject.dkmoa.service.OaPersonnelService;
import com.dkm.orproject.utils.utils.PinYingUtil;
import com.dkm.orproject.utils.utils.ThreeVo;
import com.dkm.orproject.utils.utils.Tools;
import com.github.pagehelper.PageHelper;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.Collator;
import java.util.*;

/**
 *  @ClassName : OaPersonnelSerivceImpl  //类名
 *  @Description : 实现类  //描述
 *  @Author : 石智诚  //作者
 *  @Date: 2020-07-30 09:26  //时间
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class OaPersonnelServiceImpl implements OaPersonnelService {
    /**
     * 超级管理员的id
     */
    private  final static Long ADMIN=739801243085582336L;
    /**
     * 通讯录首大写字母
     */
    private final static String UPPERCASE[]={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

    /**
     * 通讯录首小写字母
     */
    private final static String LOWERCASE[]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
    /**
     * 人员的
     */
    @Resource
    OaPersonnelMapper mapper;
    /**
     * 权限mapper
     */
    @Resource
    OaJurisdictionMapper jurisdictionMapper;
    /**
     * 部门
     */
    @Resource
    OaDepartmentMapper departmentMapper;
    /**
     * 岗位
     */
    @Resource
    OaOrganizationMapper organizationMapper;
    /**
     * 公司Mapper
     */
    @Resource
    OaCompanyMapper companyMapper;

    @Resource
    OaFlowPersonnelMapper flowPersonnelMapper;
    /**
     * 工具类
     */
    @Resource
    Tools tools;


    @Override
    public Long rowCount(OaPersonnel personnel, String startTime, String endTime) {
        return mapper.rowCount(personnel,startTime,endTime);
    }
    /**
     * 用户登录
     * @param oaPersonnel 账号和密码
     * @return 用户的信息
     */
    @Override
    public OaPersonnel isLogin(OaPersonnel oaPersonnel) { return mapper.isLogin(oaPersonnel);
    }

    /**
     * 增加用户
     * @param record 用户信息
     * @return 是否增加成功
     */

    @Override
    public int insertSelective(OaPersonnel record) {
        //密码加密
        record.setPersonnelPwd(Tools.password(record.getPersonnelPwd()));
        record.setIsRevision("1");
        return mapper.insertSelective(record);
    }
    /**
     * 根据id查询用户信息
     * @param pkPersonnelId 人员
     * @return 特定的用户信息
     */
    @Override
    public OaPersonnel selectByPrimaryKey(Long pkPersonnelId) {
        return  mapper.selectByPrimaryKey(pkPersonnelId);
    }

    /**
     * 更新用户的信息
     * @param record 用户的信息
     * @return 是否更新成功
     */
    @Override
    public int updateByPrimaryKeySelective(OaPersonnel record) {
        record.setUpdatedTime(new Date());
        return mapper.updateByPrimaryKeySelective(record);
    }

    /**
     * 逻辑删除
     * @param pkPersonnelId 人员Id
     * @return 删除的数据量
     */
    @Override
    public int updateById(Long pkPersonnelId,String personnelName) {
        return mapper.updateById(pkPersonnelId, new Date(),personnelName);
    }

    /**
     * 删除
     * @param pkPersonnelId 用户ID
     * @return 是否删除成功
     */
    @Override
    public int deleteByPrimaryKey(Long pkPersonnelId) {
        return mapper.deleteByPrimaryKey(pkPersonnelId);
    }
    /**
     * 查询所有数据并进行分页
     * @param oaPersonnel 用户信息
     * @param page 页数
     * @param limit 返回最大的记录行数目
     * @param startTime 开始时间
     * @param endTime 结束的时间
     * @return 分页的数据
     */
    @Override
    public List<OaPersonnel> findAll(OaPersonnel oaPersonnel, Integer page, Integer limit, String startTime, String endTime) {
        PageHelper.startPage(page,limit);
        return mapper.findAll(oaPersonnel, startTime, endTime);
    }
    /**
     * 批量逻辑删除
     * @param list 人员id的集合
     * @param personnelName 更新人的名字
     * @return 删除的行数
     */
    @Override
    public int updateAll(List<Long> list,String personnelName) {
        return mapper.updateAll(list, personnelName,new Date());
    }
    /**
     * 批量物理删除
     * @param list 人员id的集合
     * @return 删除的行数
     */
    @Override
    public int deleteAll(List<Long> list) {
        return mapper.deleteAll(list);
    }

    @Override
    public List<OaPersonnel> findByAll() {
        return mapper.findByAll();
    }

    @Override
    public int findByAccount(OaPersonnel personnel) {
        return  mapper.findByAccount(personnel);
    }

    @Override
    public OaPersonnel selectByPersonId(Long pkPersonnelId) {
        return mapper.selectByPersonId(pkPersonnelId);
    }

    @Override
    public List<OaPersonnel> selectByDertment(Long organizationId, Long pkPersonnelId) {
        return mapper.selectByDertment(organizationId,pkPersonnelId);
    }

    @Override
    public List<OaJurisdiction> selectByOj(Long pkPersonnelId) {
        if (pkPersonnelId.equals(ADMIN)){
            return jurisdictionMapper.findByAdmin();
        }else {
            return mapper.selectByJurisdiction(pkPersonnelId);
        }

    }

    @Override
    public List<ThreeVo> treePersons(Long flowId) {
        List<OaFlowPersonnel> flowPersonnels= flowPersonnelMapper.findAll(flowId);
        //查出来所有的人员
        List<OaPersonnel> personnels = mapper.findByAll();
        //根据所有部门
        List<OaDepartment> byAll = departmentMapper.findByAll();

        List<ThreeVo> trees=new ArrayList<>();
        for (OaDepartment department : byAll) {
            ThreeVo threeVo=new ThreeVo();
            threeVo.setSuperId(0L);
            // 菜单名称
            threeVo.setName(department.getDepartmentName());
            // 菜单id
            threeVo.setValue(department.getPkDepartmentId());
            trees.add(threeVo);
        }
        for (OaPersonnel personnel : personnels) {
            ThreeVo threeVo=new ThreeVo();
            if(personnel.getOrganizationId()==null || personnel.getOrganizationId() < 1L){
                threeVo.setSuperId(0L);
            }else{
                threeVo.setSuperId(personnel.getOrganizationId());
            }
            for (OaFlowPersonnel flowPersonnel : flowPersonnels) {
                if(personnel.getPkPersonnelId().equals(flowPersonnel.getPersonnelId())){
                    threeVo.setSelected(true);
                    break;
                }
            }
            threeVo.setName(personnel.getPersonnelName());
            threeVo.setValue(personnel.getPkPersonnelId());
            trees.add(threeVo);
        }
        return tools.createTree(trees);
    }

    /**
     * 流程id查询拥有入职流程审核的人员树
     *  @Author : 易嵘 //作者
     *  @Date: 2020-09-23 10:10  //时间
     * @param flowId 流程id
     * @param wordKey 权限关键字
     * @return 人员树
     */
    @Override
    public List<ThreeVo> findTreeEntryCheckPersons(Long flowId, String wordKey) {
        // 查询已关联的人员
        List<OaFlowPersonnel> flowPersonnels= flowPersonnelMapper.findAll(flowId);
        //查询所有的人员
        List<OaPersonnel> personnels = mapper.findByEntryCheckPersonnel(wordKey);
        //查询所有部门
        List<OaDepartment> byAll = departmentMapper.findByEntryCheckDepartment(wordKey);

        List<ThreeVo> trees=new ArrayList<>();
        for (OaDepartment department : byAll) {
            ThreeVo threeVo=new ThreeVo();
            threeVo.setSuperId(0L);
            // 菜单名称
            threeVo.setName(department.getDepartmentName());
            // 菜单id
            threeVo.setValue(department.getPkDepartmentId());
            trees.add(threeVo);
        }
        for (OaPersonnel personnel : personnels) {
            ThreeVo threeVo=new ThreeVo();
            if(personnel.getOrganizationId()==null || personnel.getOrganizationId() < 1L){
                threeVo.setSuperId(0L);
            }else{
                threeVo.setSuperId(personnel.getOrganizationId());
            }
            for (OaFlowPersonnel flowPersonnel : flowPersonnels) {
                if(personnel.getPkPersonnelId().equals(flowPersonnel.getPersonnelId())){
                    threeVo.setSelected(true);
                    break;
                }
            }
            threeVo.setName(personnel.getPersonnelName());
            threeVo.setValue(personnel.getPkPersonnelId());
            trees.add(threeVo);
        }
        return tools.createTree(trees);
    }

    @Override
    public List<OaPersonnel> export() {
        return mapper.export();
    }

    /**
     * 通讯录
     * @return 通讯录集合
     * @Author : zmh 
     * @Date: 2020-11-5 16:10  
     */
    @Override
    public List<OaAdressBookVo> addressBook() throws BadHanyuPinyinOutputFormatCombination {

        //查询所有人员信息
        List<OaPersonnel> personnels =mapper.export();
        //将查询的信息进行字母排序
            //获取中文环境
            Comparator comparator = Collator.getInstance(Locale.CHINA);
              //进行排序
            Collections.sort(personnels, (p1, p2) -> {
                return comparator.compare(p1.getPersonnelName(), p2.getPersonnelName());
            });
              //personnels.stream().forEach(System.out::println);
              //定义集合oaAdressBookDtVos，用来装数据库查询库便利后的数据复制到OaAdressBookDtVo
        List<OaAdressBookDtVo> oaAdressBookDtVos = new ArrayList<OaAdressBookDtVo>();
        for (OaPersonnel personnel : personnels) {
            OaAdressBookDtVo oaAdressBookDtVo = new OaAdressBookDtVo();
            oaAdressBookDtVo.setId(personnel.getPkPersonnelId());
            oaAdressBookDtVo.setName(personnel.getPersonnelName());
            oaAdressBookDtVo.setPhone(personnel.getPersonnelPhone());
            oaAdressBookDtVo.setDepartmentName(personnel.getDepartmentName());
            oaAdressBookDtVo.setEmail(personnel.getPersonnelEmail());
            System.out.println(String.valueOf(PinYingUtil.getFristNamePinYing(personnel.getPersonnelName())));
            oaAdressBookDtVo.setPid(String.valueOf(PinYingUtil.getFristNamePinYing(personnel.getPersonnelName())));
            oaAdressBookDtVos.add(oaAdressBookDtVo);
        }
        //定义集合oaAdressBookVos，赋予初始值大小写A-Z;
        List<OaAdressBookVo> oaAdressBookVos = new ArrayList<>();
        for (int i = 0; i<UPPERCASE.length;i++){
           OaAdressBookVo oaAdressBookVo = new OaAdressBookVo();
           oaAdressBookVo.setId(String.valueOf(i));
           oaAdressBookVo.setUppercase(UPPERCASE[i]);
           oaAdressBookVo.setLowercase(LOWERCASE[i]);
           oaAdressBookVos.add(oaAdressBookVo);
        }
        //双重循环通过oaAdressBookVos.getUppercase() 和 oaAdressBookDtVo.getPid()是否相等
        // 把数据oaAdressBookDtVo加入到oaAdressBookDtVo对象的data（集合）属性中
        for (OaAdressBookVo oaAdressBookVo : oaAdressBookVos) {
            for (OaAdressBookDtVo oaAdressBookDtVo : oaAdressBookDtVos) {
                   if(oaAdressBookDtVo.getPid().equals(oaAdressBookVo.getUppercase())){
                       oaAdressBookVo.getData().add(oaAdressBookDtVo);
                   }
            }
        }
        //返回根据人员名称第一个首写大写字母排序的OaAdressBookVo集合
        return oaAdressBookVos;

    }

    @Override
    public List<ThreeVo> selectAll() {
       // 查询所有有效性的人员
       List<OaPersonnel> personnels=mapper.selectAll();
       //查询所有有效性的岗位
       List<OaOrganization> organizations=organizationMapper.selectAll();
        //   查询所有有效性的部门
        List<OaDepartment> departments=departmentMapper.selectAll();
        //查询所有有效性的公司
        List<OaCompany> companyList=companyMapper.findByAll();
        List<ThreeVo>threeVos= new ArrayList<>();
        for (OaCompany company: companyList){
            ThreeVo threeVo=new ThreeVo();
            threeVo.setSuperId(0L);
            threeVo.setName(company.getCompanyName());
            threeVo.setValue(company.getPkCompanyId());
            threeVos.add(threeVo);
        }

        for (OaDepartment department: departments){
            ThreeVo threeVo=new ThreeVo();
            if (department.getDepartmentSuperior()==null || department.getDepartmentSuperior()< 1L ){
                threeVo.setSuperId(0L);
            }else {
                threeVo.setSuperId(department.getDepartmentSuperior());
                threeVo.setValue(department.getPkDepartmentId());
                threeVo.setName(department.getDepartmentName());
            }
            threeVos.add(threeVo);
        }
        for (OaOrganization  organization:organizations){
            ThreeVo threeVo=new ThreeVo();
            if (organization.getDepartmentId()==null || organization.getDepartmentId()<1L){
                threeVo.setSuperId(0L);
            }else {
                threeVo.setSuperId(organization.getDepartmentId());
                threeVo.setValue(organization.getPkPostId());
                threeVo.setName(organization.getPostName());
            }
            threeVos.add(threeVo);
        }
        for (OaPersonnel personnel : personnels) {
            ThreeVo threeVo=new ThreeVo();
                if (personnel.getOrganizationType()==null || personnel.getOrganizationType()<1L){
                    threeVo.setSuperId(0L);
                }else {
                    threeVo.setSuperId(personnel.getOrganizationType());
                    threeVo.setValue(personnel.getPkPersonnelId());
                    threeVo.setName(personnel.getPersonnelName());
                }
                threeVos.add(threeVo);
            }
        return tools.createTree(threeVos);
    }


}
